package org.zachary.prosocialbehaviorservice.domain.aggregate_model.activity_user;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.domain.AbstractAggregateRoot;
import org.zachary.prosocialbehaviorservice.domain.aggregate_model.activity.ActivityItem;
import org.zachary.prosocialbehaviorservice.domain.aggregate_model.activity_user.event.JoinActivityEvent;

import java.io.Serial;
import java.io.Serializable;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name="user_activity")
@Data
public class UserActivity extends AbstractAggregateRoot<UserActivity> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private Integer userId;
    private Integer activityId;
    private boolean finished;
    private Instant createTime;
    private Instant updateTime;


    @Transient
    private List<ActivityItem> items = new ArrayList<>();
    protected UserActivity() {}
    private UserActivity(Integer userId, Integer activityId, boolean finished, Instant createTime, Instant updateTime) {
        this.userId = userId;
        this.activityId = activityId;
        this.finished = finished;
        this.createTime = createTime;
        this.updateTime = updateTime;
    }
    public static UserActivity create(Integer userId, Integer activityId, boolean finished, Instant createTime, Instant updateTime) {
        return new UserActivity(userId, activityId, finished, createTime, updateTime);
    }

    public void join() {
        registerEvent(new JoinActivityEvent(this));
    }

}
